package com.suneast.suneaststore.ui.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suneast.suneaststore.R;

/**
 * Created by zestz on 2018/3/8.
 */

public class TitleView extends RelativeLayout {

    private TextView titleNameTv;
    private TextView titleIntroTv;
    private TextView moreInfoTv;

    private int lineColor = Color.BLACK;
    private int titleNameColor = Color.BLACK;
    private int titleIntroColor = Color.DKGRAY;
    private String titleName = "TitleName";
    private String titleIntro = "TitleIntro";
    private boolean isIntroVisible = true;
    private boolean isMoreInfoVisible = false;


    public TitleView(Context context) {
        super(context);
        initView(context);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        lineColor = mTypedArray.getColor(R.styleable.TitleView_title_line_color, Color.BLACK);
        titleNameColor = mTypedArray.getColor(R.styleable.TitleView_title_name_text_color, Color.BLACK);
        titleIntroColor = mTypedArray.getColor(R.styleable.TitleView_title_intro_text_color, Color.DKGRAY);
        titleName = mTypedArray.getString(R.styleable.TitleView_title_name_text);
        titleIntro = mTypedArray.getString(R.styleable.TitleView_title_intro_text);
        isIntroVisible = mTypedArray.getBoolean(R.styleable.TitleView_show_intro, true);
        isMoreInfoVisible = mTypedArray.getBoolean(R.styleable.TitleView_show_more, false);
        //获取资源后要及时回收
        mTypedArray.recycle();
        initView(context);

    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);

    }

    private void initView(Context ctx) {
        LayoutInflater.from(ctx).inflate(R.layout.layout_common_title, this, true);
        titleNameTv = findViewById(R.id.title_name_tv);
        ImageView leftImgV = findViewById(R.id.left_image_view);
        ImageView rightImgV = findViewById(R.id.right_image_view);
        titleIntroTv = findViewById(R.id.title_intro_tv);
        moreInfoTv = findViewById(R.id.more_info_tv);
        moreInfoTv.setVisibility(isMoreInfoVisible ? View.VISIBLE : View.INVISIBLE);
        moreInfoTv.setOnClickListener((View v) -> {
            if (listener != null) {
                listener.onMoreInfoClick();
            }
        });
        titleNameTv.setText(titleName);
        titleNameTv.setTextColor(titleNameColor);
        titleIntroTv.setVisibility(isIntroVisible ? VISIBLE : GONE);
        titleIntroTv.setText(titleIntro);
        titleIntroTv.setTextColor(titleIntroColor);
        leftImgV.setBackgroundColor(lineColor);
        rightImgV.setBackgroundColor(lineColor);
    }

    public void setTitleName(String name) {
        if (titleNameTv != null) {
            titleNameTv.setText(name);
        }
    }

    public void setIntro(String intro) {
        if (titleIntroTv != null) {
            titleIntroTv.setText(intro);
        }
    }

    public void setMoreInfoVisible(boolean isVisible) {
        isMoreInfoVisible = isVisible;
        moreInfoTv.setVisibility(isMoreInfoVisible ? View.VISIBLE : View.INVISIBLE);
    }

    MoreInfoClickListener listener;

    public interface MoreInfoClickListener {
        void onMoreInfoClick();
    }

    public void setMoreInfoClickListener(MoreInfoClickListener listener) {
        this.listener = listener;
    }

}
